<?php
/**
 * Created by IDEA.
 * User: dinesh
 * Date: Feb 24, 2012
 * Time: 11:49:08 AM
 */

/**
 * This file handles the retrieval and serving of news articles
 */
class NewsController extends BaseController {


    public function actionMain() {
        $author = '';
        $newsModel = new News;
        if (isset($_GET['author'])) {
            $author = urldecode($_GET['author']);
        }
        $article = $newsModel->get_article($author);
        $this->render('news', array('model' => $article,));

    }

    public function action() {
        $author = '';
        $newsModel = new News;
        if (isset($_GET['author'])) {
            $author = urldecode($_GET['author']);
        }
        $article = $newsModel->get_article($author);
        $this->renderPartial('news', array('model' => $article,));

    }

    public function actionError() {
        $this->renderPartial('errorPage');

    }

    public function actionGrid() {
        $this->render('newsgrid');
    }


    public function actionGetdata() {
        mysql_connect('localhost', 'root', '123');
        mysql_select_db('shnims_db');
        $sql = "select census_id, school_name, address_line1 from school_tbl limit 500 ,10";
        $data = array();
        $results = mysql_query($sql);

        while ($row = mysql_fetch_assoc($results)) {
            $data['rows'][] = array(
                'id' => $row['census_id'],
                'data' => array($row['census_id'], $row['school_name'], $row['address_line1'])
            );
        }

        $this->renderPartial('newsdata', array('model' => $data,));
    }

    public function actionPgrid() {
        $this->render('newspgrid');
    }

    public function actionPgridData() {
        //set content type and xml tag
        header("Content-type:text/xml");
        print("<?xml version=\"1.0\"?>");

        //define variables from incoming values
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;

        //connect to database
        $link = mysql_pconnect("localhost", "root", "123");
        $db = mysql_select_db("shnims_db");

        //create query to products table
        $sql = "SELECT  * FROM school_tbl";
        if (isset($_GET["name_mask"]))
            $sql .= " Where school_name like '" . $_GET["name_mask"] . "%'";


        //if this is the first query - get total number of records in the query result
        if ($posStart == 0) {
            $sqlCount = "Select count(*) as cnt from ($sql) as tbl";
            $resCount = mysql_query($sqlCount);
            $rowCount = mysql_fetch_array($resCount);
            $totalCount = $rowCount["cnt"];
        }
        //order by
        $columns = array("school_id", "school_name", "address_line1");
        if (isset($_GET["orderby"])) {
            if ($_GET["direct"] == 'des')
                $direct = "DESC";
            else
                $direct = "ASC";
            $sql .= " Order by " . $columns[$_GET["orderby"]] . " " . $direct;
        }
        //add limits to query to get only rows necessary for the output
        $sql .= " LIMIT " . $posStart . "," . $count;

        //query database to retrieve necessary block of data
        $res = mysql_query($sql);

        //output data in XML format
        print("<rows total_count='" . $totalCount . "' pos='" . $posStart . "'>");
        while ($row = mysql_fetch_array($res)) {
            print("<row id='" . $row['census_id'] . "'>");
            print("<cell>");
            print($row['school_id']); //value for product name
            print("</cell>");
            print("<cell>");
            print($row['school_name']); //value for internal code
            print("</cell>");
            print("<cell>");
            print($row['address_line1']); //value for price
            print("</cell>");
            print("</row>");
        }
        print("</rows>");
    }

    public function actionGrid1() {
        $this->renderPartial('paging');
    }

    public function actionPaging() {

        error_reporting(E_ALL ^ E_NOTICE);

        header("Content-type:text/xml");
        ini_set('max_execution_time', 600);
        //require_once('../../common/config.php');
        print("<?xml version=\"1.0\"  encoding=\"ISO-8859-1\"?>");

        function microtime_float() {
            list($usec, $sec) = explode(" ", microtime());
            return ((float) $usec + (float) $sec);
        }

        GLOBAL $posStart, $count, $nm_mask, $cd_mask;


        //$_GET['posStart'];
        if (isset($_GET["posStart"]))
            $posStart = $_GET['posStart'];
        else
            $posStart = 0;
        if (isset($_GET["count"]))
            $count = $_GET['count'];
        else
            $count = 100;
        if (isset($_GET["nm_mask"]))
            $nm_mask = $_GET['nm_mask'];
        else
            $nm_mask = "";
        if (isset($_GET["cd_mask"]))
            $cd_mask = $_GET['cd_mask'];
        else
            $cd_mask = "";

        //$_GET['count'];
        $mysql_host = "localhost";
        $mysql_user = "root";
        $mysql_pasw = "123";
        $mysql_db = "shnims_db";
        $link = mysql_pconnect($mysql_host, $mysql_user, $mysql_pasw);
        $db = mysql_select_db($mysql_db);

        if (!isset($_GET["orderBy"]))
            $_GET["orderBy"] = 0;
        if (!isset($_GET["direction"]) || $_GET["direction"] == "asc")
            $_GET["direction"] = "ASC";
        else
            $_GET["direction"] = "DESC";


        $fields = array("school_id", "", "address_line1");
        $this->getDataFromDB('', '', $fields[$_GET["orderBy"]], $_GET["direction"]);
        mysql_close($link);


        //print one level of the tree, based on parent_id

    }

    function getDataFromDB($name_mask, $code_mask, $sort_by, $sort_dir) {
        GLOBAL $posStart, $count, $nm_mask, $cd_mask;
        $sql = "SELECT  * FROM school_tbl Where 0=0";
        if ($nm_mask != '')
            $sql .= " and school_id like '$nm_mask%'";
        if ($cd_mask != '')
            $sql .= " and address_line1 like '$cd_mask%'";
        if ($sort_dir == '')
            $sort_dir = "asc";
        if ($sort_by != '')
            $sql .= " Order By $sort_by $sort_dir";
        //print($sql);
        //echo $sql;
        if ($posStart == 0) {
            $sqlCount = "Select count(*) as cnt from ($sql) as tbl";
            //print($sqlCount);
            $resCount = mysql_query($sqlCount);
            while ($rowCount = mysql_fetch_array($resCount)) {
                $totalCount = $rowCount["cnt"];
            }
        }
        $sql .= " LIMIT " . $posStart . "," . $count;
        $res = mysql_query($sql);
        print("<rows total_count='" . $totalCount . "' pos='" . $posStart . "'>");
        if ($res) {
            while ($row = mysql_fetch_array($res)) {
                print("<row id='" . $row['census_id'] . "'>");
                print("<cell>");
                print($row['school_id']); //."[".$row['item_id']."]");
                print("</cell>");
                print("<cell>");
                print($row['school_name']); // print("index is ".$posStart);
                print("</cell>");
                print("<cell>");
                print($row['address_line1']);
                print("</cell>");
                print("</row>");
                $posStart++;
            }
        } else {
            echo mysql_errno() . ": " . mysql_error() . " at " . __LINE__ . " line in " . __FILE__ . " file<br>" . $sql;
        }

        print("</rows>");
    }

    public function actionAdo() {
        $ado = new AdodbDriver();
        $ado->pConnect();
        $sql = 'select theme_name,id from aea_theme';
        $ado->runSQL($sql);
    }

    public function actionAdo1() {
        $ado = new AdodbDriver();
        // $ado->pConnect('mysqli','localhost','science_msc','root','123',true);
        //  $ado->autoExe();
        $ado->dnsConnect();
    }

    public function actionAr() {
        $ado = new AdodbDriver();
        $ado->activeRecode();
    }

}
